package com.example.demo03.util;

import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;

import java.io.IOException;
import java.io.InputStream;

public class SqlSessionUtil {
    static SqlSessionFactory build = null;

    static {
        try {
            // 获取工厂类对象
            InputStream is = Resources.getResourceAsStream("mybatis-config.xml");
            build = new SqlSessionFactoryBuilder().build(is);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    private SqlSessionUtil() {
    }

    public static SqlSession createSqlSession() {
        return build.openSession();
    }

    public static SqlSession createSqlSession(boolean flag) {
        return build.openSession(flag);
    }
}
